package com.hxkcloud.nurse.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.request.UrlAddress;

import org.apache.http.util.ByteArrayBuffer;
import org.apache.http.util.EncodingUtils;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by william_sun on 2016-09-01.
 * Description:评估反馈测试页
 */
public class AssessFeedback1Activity extends BaseActivity {
    private WebView webView;
    String assessId;
    String questionId;
    String url;

    @Override
    protected void updateByHandler(Message msg) {
        if (msg.what == 1111) {
            webView.loadData((String) msg.obj, "text/html; charset=UTF-8", null);
            webView.measure(0, 0);
            System.out.println("getHeight:" + webView.getHeight());
            System.out.println("getContentHeight:" + webView.getContentHeight());
            System.out.println("getMeasuredHeight:" + webView.getMeasuredHeight());
        }
        if (msg.what == 2222) {
            webView.setVisibility(View.VISIBLE);
            webView.measure(0, 0);
            System.out.println("FgetHeight:" + webView.getHeight());
            System.out.println("FgetContentHeight:" + webView.getContentHeight());
            System.out.println("FgetMeasuredHeight:" + webView.getMeasuredHeight());
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_assessfeedback1);
        Intent iGet = getIntent();
        assessId = iGet.getStringExtra("assessId");
        questionId = iGet.getStringExtra("questionUserId");

        webView = (WebView) findViewById(R.id.detail_webview);
        webView.getSettings().setJavaScriptEnabled(true);

//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING);
//        } else {
//            webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
//        }
        url = UrlAddress.H5_QUESTIONASSESS_URL + "&isEdit=2" + "&questionId=" + assessId + "&questionUserId=" + questionId + "&userId=" + loginInfo.getUserId() + "&acctType=" + loginInfo.getUserStyle();

//        new Thread() {
//            @Override
//            public void run() {
//                super.run();
////                String html = getHtmlData(getHtmlString(url));
//                String html = getHtmlString(url).replace("class=\"content native-scroll\"", "class=\"content native-scroll\" style=\"overflow: visible;\"");
//                Message msg = new Message();
//                msg.what = 1111;
//                msg.obj = html;
//                handler.sendMessage(msg);
//
//            }
//        }.start();

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

                System.out.println("pagefinish");
//                System.out.println("getHeight:" + view.getHeight());
//                System.out.println("getContentHeight:" + view.getContentHeight());
                handler.sendEmptyMessage(2222);
            }

        });

        webView.loadUrl(url);

    }

    private String getHtmlData(String bodyHTML) {
        String head = "<head>" +
                "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> " +
                "<style>img{max-width: 100%; width:auto; height:auto;}</style>" +
                "</head>";
        return "<html>" + head + "<body>" + bodyHTML + "</body></html>";
    }


    private String getHtmlString(String urlString) {
        try {
            URL url = new URL(urlString);
            URLConnection ucon = url.openConnection();
            InputStream instr = ucon.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(instr);
            ByteArrayBuffer bau = new ByteArrayBuffer(500);
            int current = 0;
            while ((current = bis.read()) != -1) {
                bau.append((byte) current);
            }
            return EncodingUtils.getString(bau.toByteArray(), "utf-8");
        } catch (Exception e) {
            return "";
        }
    }


    @Override
    protected void initView() {

    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }
}
